<?php
/**
 * Created by PhpStorm.
 * User: YSF
 * Date: 2017\10\27 0027
 * Time: 9:45
 */

namespace app\api\controller;

use think\Db;

/**
 * Class Getnumber
 * @package app\api\controller
 *       业绩系统新增员工在这获取编号
 */
class Getnumber extends RestBase
{

    /**
     * @return \think\response\Json
     *      根据请求，返回员工编号
     */
    public function getNumber()
    {
        // 接收请求参数
        $card = request()->post('card');
        if (!empty($card)) {
            // 根据请求参数获取员工编号
            $where['id_card'] = $card;
            $order = [
                'is_entry'=>'ASC'
            ];
            $customerData = Db::name('customer')->where($where)->order($order)->find();
            if (empty($customerData)) {
                return json(['code' => 0, 'message' => '身份证号码不存在']);
            }
            if (empty($customerData['employee_id']) || $customerData['is_entry'] != 1){
                return json(['code' => 0, 'message' => '该员工未入职不能添加']);
            }
            // 根据员工编号获取入职时间
            $personnel_number = Db::name('personnel_number')->where(['personnel_number' => $customerData['employee_id']])->find();
            if (empty($personnel_number) || empty($personnel_number['personnel_time'])) {
                return json(['code' => 0, 'message' => '该员工编号缺失，请联系管理员']);
            }
            $customerData['personnel_time'] = $personnel_number['personnel_time'];
                return json(['code' => 1, 'number' => $customerData]);
        }else{
                return json(['code' => 0, 'message' => '请输入身份证号码']);
        }
    }

    /**
     * @return \think\response\Json
     *       根据请求，查询员工编号是否存在
     */
    public function queryNumber()
    {
        // 接收请求参数
        $number = request()->get('number');
        $where['personnel_number'] = $number;
        // 查询员工编号是否存在
        $result = Db::name('personnel_number')->where($where)->find();
        if (!$result) {
            return json(['code' => 1, 'message' => '员工编号可以使用']);
        }
        return json(['code' => 0, 'message' => '员工编号已存在']);
    }

}